<?php
require 'include/library.inc.php';
require 'include/user_login.inc.php';
require 'include/paging.inc.php';

# Submit
if ($_POST['is_submitted']) {
    $error_message = '';	
	
	# Validations
	if (!$_POST['first_name'])  $error_message .= 'First Name is required<br/>';
	if (!$_POST['last_name'])  $error_message .= 'Last Name is required<br/>';
	if (!$_POST['date_of_birth'])  $error_message .= 'Date of birth is required<br/>';
	if (!$_POST['phone'])  $error_message .= 'Phone number is required<br/>';
	if (!$_POST['gender'])  $error_message .= 'Gender is required<br/>';
	if (!$_POST['hometown_country_id'])  $error_message .= 'Hometown Country is required<br/>';
	if (!$_POST['street_address'])  $error_message .= 'Street Address is required<br/>';
	if (!$_POST['city'])  $error_message .= 'City is required<br/>';
	if (!$_POST['zipcode'])  $error_message .= 'Zip Code is required<br/>';
	if (!$_POST['country_id'])  $error_message .= 'Country is required<br/>';
	if (!$_POST['english_level'])  $error_message .= 'English Level is required<br/>';
	if (!$_POST['future_plans'])  $error_message .= 'What are your plans after completing this course?<br/>';

	
	# OK
    if(!$error_message) {
		
		# Update the user
		$sql = 'UPDATE users
				SET
					first_name = \''.escape($_POST['first_name']).'\',
					last_name = \''.escape($_POST['last_name']).'\',
					phone = \''.escape($_POST['phone']).'\',
					date_of_birth = \''.escape($_POST['date_of_birth']).'\',
					gender  = \''.escape($_POST['gender']).'\',
					married = '.intval($_POST['married']).',
					hometown_country_id = '.intval($_POST['hometown_country_id']).',
					street_address = \''.escape($_POST['street_address']).'\',
					city = \''.escape($_POST['city']).'\',
					state = \''.escape($_POST['state']).'\',
					country_id = '.intval($_POST['country_id']).',
					zipcode = \''.escape($_POST['zipcode']).'\',
					emergency_contact = \''.escape($_POST['emergency_contact']).'\',
					high_school_name = \''.escape($_POST['high_school_name']).'\',
					high_school_date = \''.escape($_POST['high_school_date']).'\',
					university_name = \''.escape($_POST['university_name']).'\',
					university_date = \''.escape($_POST['university_date']).'\',
					english_level = '.intval($_POST['english_level']).',
					toefl = \''.escape($_POST['toefl']).'\',
					future_plans = '.intval($_POST['future_plans']).',
					finances = '.intval($_POST['finances']).',
					in_us = '.intval($_POST['in_us']).',
					have_visa = '.intval($_POST['have_visa']).',
					visa_type = \''.escape($_POST['visa_type']).'\',
					attending_school = '.intval($_POST['attending_school']).',
					i20_institution = \''.escape($_POST['i20_institution']).'\',
					i20_id = \''.escape($_POST['i20_id']).'\'
				WHERE
					user_id = '.$_SESSION['USER_SID'].'
				';
		query($sql);
				
		
		$_SESSION['message'] = 'The information was updated!';
		redirect(HTML_BASE_SSL."myprofile.php");
						
		
	} else { # Error
		
		$row_user =& $_POST;
		
	}
	
	
} else { # Initial Loading

	$sql = 'SELECT
				*
			FROM
				users
			WHERE
				user_id = '.$_SESSION['USER_SID'].'
			';
	$row_user = mysql_fetch_assoc(query($sql));	

}

head($row_user['first_name']."'s Profile","",100);
?>
<script type="text/javascript" src="<?=HTML_BASE_ADMIN?>js/calendarDateInput.js"></script>

    	<div id="content">
        	
            <a href="myprofile.php">&laquo; Back to My Profile</a><br><br>
            
            <?php if ($error_message) { ?>
              <div class="message red">
                <h4>Please complete the required fields!</h4><br>
                <?=$error_message?>
                <br>
              </div>
              <br>
            <?php } ?>
                        
            <div class="padding">
        	
            
            <form action="<?=HTML_BASE_SSL.$_SERVER['PHP_SELF']?>" method="post" enctype="multipart/form-data">
            <input type="hidden" name="is_submitted" value="1" />            
            
            <h2>Personal Information Edit</h2><br>
            
            <table class="application" width="100%">
            <tr><td width="50%">
            
            <b>Fisrt Name</b><br>
            <input type="text" class="inputbox w300" name="first_name" value="<?=$row_user['first_name']?>" />
            </td><td>
            
            <b>Last Name</b><br>
            <input type="text" class="inputbox w300" name="last_name" value="<?=$row_user['last_name']?>" />
            
            </td></tr>
            <tr><td>
            
            <b>Date Of Birth</b><br>
            <script>DateInput('date_of_birth',true,'YYYY-MM-DD',
    		'<?=(($row_user['date_of_birth'])?date('Y-m-d',strtotime($row_user['date_of_birth'])):date('Y-m-d'))?>')</script>
            
            
            </td><td>
            
            <b>Phone Number</b><br>
            <input type="text" class="inputbox w300" name="phone" value="<?=$row_user['phone']?>" />
              
            </td></tr>
            <tr><td>
            
            <br>
            <b>Gender</b>
            <label>
              <input type="radio" name="gender" value="m" id="gender_0" <?=($row_user['gender']=='m')?"checked":""?> />
              Male</label>
            <label>
              <input type="radio" name="gender" value="f" id="gender_1" <?=($row_user['gender']=='f')?"checked":""?> />
              Female</label>
            <br><br>

            
            </td><td>
            
            <br>
            <b>Marital Status</b>
            <label>
              <input type="radio" name="married" value="0" id="married_1" <?=($row_user['married']==0)?"checked":""?>/>
              Single</label>
            <label>
              <input type="radio" name="married" value="1" id="married_0" <?=($row_user['married']==1)?"checked":""?> />
              Married</label>
            <br><br>


            </td></tr>
            <tr><td>
            <b>Hometown Country</b><br>
            <select class="inputbox w300" name="hometown_country_id">
            <option value="">-- Choose --</option>
            <?php
			$sql = "SELECT
						country_id,
						name
					FROM
						countries
					ORDER BY
						country_id
					  ";
			$result = query($sql);
            while ($row_country = mysql_fetch_object($result)) {
				echo '<option value="'.$row_country->country_id.'" '.(($row_user['hometown_country_id']==$row_country->country_id)?' selected':'').'>'.$row_country->name.'</option>';
			}
			?>
            </select>
            </td><td>&nbsp;
                        
            </td></tr>
            
            </table><br><br>
            
            <h2>Contact Information</h2><br>

            <small>Mailing Address (where official documents will be sent by school)</small>
            
            <br><br>
            <table class="application" width="100%">
            <tr><td width="50%">
            
            <b>Street Address</b><br>
            <input type="text" class="inputbox w300" name="street_address" value="<?=$row_user['street_address']?>" />
            </td><td>
            
            <b>City</b><br>
            <input type="text" class="inputbox w300" name="city" value="<?=$row_user['city']?>" />
            
            </td></tr>
            <tr><td>
            
            <b>State</b><br>
            <input type="text" class="inputbox w300" name="state" value="<?=$row_user['state']?>" />
            
            </td><td>
            
            <b>Zip Code</b><br>
            <input type="text" class="inputbox w300" name="zipcode" value="<?=$row_user['zipcode']?>" />
            
            </td></tr>
            <tr><td>
            
            <b>Country</b><br>
            <select class="inputbox w300" name="country_id">
            <option value="">-- Choose --</option>
            <?php
			$sql = "SELECT
						country_id,
						name
					FROM
						countries
					ORDER BY
						country_id
					  ";
			$result = query($sql);
            while ($row_country = mysql_fetch_object($result)) {
				echo '<option value="'.$row_country->country_id.'" '.(($row_user['country_id']==$row_country->country_id)?' selected':'').'>'.$row_country->name.'</option>';
			}
			?>
            </select>
            
            </td><td>
            
            <b>Emergency Contact</b> <small>(Parent's Name and Phone)</small><br>
            <input type="text" class="inputbox w300" name="emergency_contact" value="<?=$row_user['emergency_contact']?>" />
            
            </td></tr>
            
            </table><br><br>
            
            <h2>Educational Background</h2><br>
            
            <table class="application" width="100%">
            <tr><td width="50%">
            
            <b>Have you graduated from high school?</b><br>
			<small>Enter School Name and Year</small><br>
            <input type="text" class="inputbox w300" name="high_school_name" value="<?=$row_user['high_school_name']?>" />
            
            <select class="inputbox" name="high_school_date">
            <?php
            for ($i=date('Y');$i>=1930;$i--) {
				echo '<option value="'.$i.'" '.(($row_user['high_school_date']==$i)?'selected':'').'>'.$i.'</option>';
			}
			?>
            </select>
            </td><td>
            
            <b>Have you graduated from college or university? </b><br>
			<small>Enter School Name and Year</small><br>
            <input type="text" class="inputbox w300" name="university_name" value="<?=$row_user['university_name']?>" />
            
            <select class="inputbox" name="university_date">
            <?php
            for ($i=date('Y');$i>=1930;$i--) {
				echo '<option value="'.$i.'" '.(($row_user['university_date']==$i)?'selected':'').' >'.$i.'</option>';
			}
			?>
            </select>
            
            </td></tr>
            <tr><td>
            <br>
            <b>English Level</b><br>
              <label>
                <input type="radio" name="english_level" value="1" id="english_0" <?=($row_user['english_level']==1)?"checked":""?> >
                Beginner</label>
              <label>
                <input type="radio" name="english_level" value="2" id="english_1" <?=($row_user['english_level']==2)?"checked":""?> >
                Elementary</label>
              <label>
                <input type="radio" name="english_level" value="3" id="english_2" <?=($row_user['english_level']==3)?"checked":""?> >
                Low Intermediate</label><br>
              <label>
                <input type="radio" name="english_level" value="4" id="english_3" <?=($row_user['english_level']==4)?"checked":""?> >
                High Intermediate</label>
              <label>
                <input type="radio" name="english_level" value="5" id="english_4" <?=($row_user['english_level']==5)?"checked":""?> >
                Advanced</label>
                
            </td><td>
            
            <b>Have you taken the TOEFL within the past 2 years?</b><br>
            <small>Enter Test Date and Score</small>
            <input type="text" class="inputbox w300" name="toefl" value="<?=$row_user['toefl']?>" />
            
            </td></tr>
            <tr><td colspan="2" >
            
            <br>
            <b>What are your plans after completing this course?</b><br>
              <label>
                <input type="radio" name="future_plans" value="1" id="future_plans_0" <?=($row_user['future_plans']==1)?"checked":""?> >
                Study at University or College</label>
              <label>
                <input type="radio" name="future_plans" value="2" id="future_plans_1" <?=($row_user['future_plans']==2)?"checked":""?> >
                Return to my country</label>
              <label>
                <input type="radio" name="future_plans" value="3" id="future_plans_2" <?=($row_user['future_plans']==3)?"checked":""?> >
                Undecided</label>
              <label>
                <input type="radio" name="future_plans" value="4" id="future_plans_3" <?=($row_user['future_plans']==4)?"checked":""?> >
                Other</label>
            
            </td></tr>
            <tr><td colspan="2" >
            
            <br>
            <b>Who will financially support your study abroad?</b><br>
              <label>
                <input type="radio" name="finances" value="1" id="finances_0" <?=($row_user['finances']==1)?"checked":""?> >
                Self-funded</label>
              <label>
                <input type="radio" name="finances" value="2" id="finances_1" <?=($row_user['finances']==2)?"checked":""?> >
                Family</label>
              <label>
                <input type="radio" name="finances" value="3" id="finances_2" <?=($row_user['finances']==3)?"checked":""?> >
                Company</label>
              <label>
                <input type="radio" name="finances" value="4" id="finances_3" <?=($row_user['finances']==4)?"checked":""?> >
                School</label>
              <label>
                <input type="radio" name="finances" value="5" id="finances_4" <?=($row_user['finances']==5)?"checked":""?> >
                Scholarship</label>
              <label>
                <input type="radio" name="finances" value="6" id="finances_5" <?=($row_user['finances']==6)?"checked":""?> >
                Other</label>
            
            </td></tr>
            
            </table>
            <br><br>
                        
            <h2>Visa Information (US only)</h2><br>
            
            <table class="application" width="100%">
            <tr><td colspan="2" >
            
            <b>Are you currently in the U.S.?</b><br>
            <label>
            <input type="radio" name="in_us" value="1" id="in_us_0" <?=($row_user['in_us']==1)?"checked":""?> >
            Yes</label>
            <label>
            <input type="radio" name="in_us" value="0" id="in_us_1" <?=($row_user['in_us']==0)?"checked":""?> >
            No</label>
            
            </td></tr>
            
            <tr><td width="50%">
            <br>
            <b>Do you currently have a visa?</b><br>
              <label>
                <input type="radio" name="have_visa" value="1" id="have_visa_0" <?=($row_user['have_visa']==1)?"checked":""?> >
                Yes</label>
              <label>
                <input type="radio" name="have_visa" value="0" id="have_visa_1" <?=($row_user['have_visa']==0)?"checked":""?> >
                No</label>
            
            </td><td>
            
            <b>What is your visa / immigration status?</b><br>
            <small>F-1/2, B-1/2, J-1/2, Permanent Resident, Citizenship, Other</small>
            <input type="text" class="inputbox w300" name="visa_type" value="<?=$row_user['visa_type']?>" />
            
            </td></tr>
            
            <tr><td colspan="2">
            <b>Are you currently attending school in the U.S.?</b><br>
              <label>
                <input type="radio" name="attending_school" value="1" id="attending_school_0" <?=($row_user['attending_school']==1)?"checked":""?> >
                Yes</label>
              <label>
                <input type="radio" name="attending_school" value="0" id="attending_school_1" <?=($row_user['attending_school']==0)?"checked":""?> >
                No</label>
            
            </td></tr>
            
            <tr><td>
            <br>
            <b>Name of institution issued your I-20?</b><br>
            <input type="text" class="inputbox w300" name="i20_institution" value="<?=$row_user['i20_institution']?>" />
            
            </td><td>
            <br>
            <b>SEVIS ID number of your current I-20?</b><br>
            <input type="text" class="inputbox w300" name="i20_id" value="<?=$row_user['i20_id']?>" />
            
            </td></tr>
            
            </table>
            <br>
                        
            <button type="submit" class="button">Save</button>
            
            </form>
            
            </div>
        </div>
        
            
<?php foot(); ?>